<template>
    <el-container style="height: 700px; border: 1px solid #eee">
        <!--侧边栏-->
        <component-sidebar></component-sidebar>

        <el-container>
            <!--标题-->
            <el-header style="text-align: right; font-size: 12px">
                <h1 id="pageTitle">图书管理系统</h1>
            </el-header>
            <!-- 主体 -->
            <el-main>
                <!--搜索表单-->
                <el-form id="bookSearchForm" :inline="true" :model="formInline" class="demo-form-inline">
                    <el-form-item label="图书编号：">
                        <el-input v-model="formInline.bookId" placeholder="图书编号"></el-input>
                    </el-form-item>
                    <el-form-item label="用户编号：">
                        <el-input v-model="formInline.userId" placeholder="用户编号"></el-input>
                    </el-form-item>
                    <el-form-item label="状态：">
                        <el-select v-model="formInline.status" placeholder="状态">
                            <el-option label="已归还" value="1"></el-option>
                            <el-option label="未归还" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="searchBorrow">查询</el-button>
                    </el-form-item>
                </el-form>
                <!--数据表格-->
                <el-table
                        :data="tableData"
                        border
                        style="width: 100%"
                        align="center"
                        id="table">
                    <el-table-column
                            prop="bookId"
                            label="书编"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="bookName"
                            label="书名"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="user"
                            label="借出者"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="userPhone"
                            label="借出者电话号码"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="status"
                            label="状态"
                            align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.status === 1">已归还</span>
                            <span v-else>未归还</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="returnDeadline"
                            label="最迟归还时间"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            prop="returnTime"
                            label="归还时间"
                            align="center">
                    </el-table-column>
                    <el-table-column
                            fixed="right"
                            label="操作"
                            align="center">
                        <template slot-scope="scope">
                            <el-button v-if="scope.row.status !== 1" @click="clickReturn(scope.row)" type="text"
                                       size="small">还书
                            </el-button>
                            <el-button v-else type="text" size="small" disabled>已归还</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页条-->
                <div class="block">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="pageNation.currentPage"
                            :page-sizes="[5, 10, 20, 30]"
                            :page-size="pageNation.pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="pageNation.total">
                    </el-pagination>
                </div>
            </el-main>

        </el-container>

    </el-container>
</template>

<style>
.el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
}

#pageTitle {
    margin: 0;
    font-size: 40px;
    text-align: center;
}

#insertBtn {
    display: flex;
    float: right;
}

.cover-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}

.cover {
    width: 100px;
    height: 100px;
    display: block;
}

.cover-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.cover-uploader .el-upload:hover {
    border-color: #409eff;
}

.bookImg {
    width: 60px;
}

</style>

<script>
import ComponentSidebar from "@/components/ComponentSidebar.vue";
import axios from "axios";

const sendRequestMethod = {
    //发请求查询图书列表
    listBorrow(pageNation, formInline) {
        let url = "/borrow/admin/borrow/page";
        url += ("?page=" + pageNation.currentPage + "&pageSize=" + pageNation.pageSize);

        if (formInline.bookId != null) {
            url += "&bookId=" + formInline.bookId;
        }
        if (formInline.userId != null) {
            url += "&userId=" + formInline.userId;
        }
        if (formInline.status != null) {
            url += "&status=" + formInline.status;
        }
        try {
            //console.log("请求url：" + url);
            axios.get(url, {
                headers: {
                    'token': sessionStorage.getItem("token")
                }
            }).then(
                    (res) => {
                        this.tableData = res.data.data.records;
                        this.pageNation.total = res.data.data.total;
                    }
            ).catch(
                    (err) => {
                        if (err.response.status === 401) {
                            //跳转登录页面
                            this.$router.push("/login");
                        } else {
                            this.openNotification("error", "错误", "查询借书记录失败");
                        }
                    }
            );
        } catch (error) {
            // 捕获OS错误或其他异常
            //console.error('Caught an error:', error);
            this.openNotification("error", "错误", "查询借书记录失败!");
        }
    },
}

export default {
    components: {ComponentSidebar},
    data() {
        const item = {
            bookId: '',
            bookName: '',
            user: '',
            userPhone: '',
            status: '',
            returnDeadline: '',
            returnTime: '',
        };
        return {
            // 表格数据
            tableData: Array(0).fill(item),
            // 搜索表单数据
            formInline: {
                bookId: null,
                userId: null,
                status: null,
            },
            //分页条属性
            pageNation: {
                currentPage: 1,
                pageSize: 5,
                total: 0
            },
            formLabelWidth: '120px',
            imageUrl: '',
        }
    },


    methods: {
        ...sendRequestMethod,

        //点击查询按钮
        searchBorrow() {
            this.listBorrow(this.pageNation, this.formInline);
        },

        //分页条pageSize改变
        handleSizeChange(val) {
            this.pageNation.pageSize = val;
            this.listBorrow(this.pageNation, this.formInline);
        },

        //分页条currentPage改变
        handleCurrentChange(val) {
            this.pageNation.currentPage = val;
            this.listBorrow(this.pageNation, this.formInline);
        },

        //弹出通知
        openNotification(type, title, message) {
            if (type === "error") {
                this.$notify.error({
                    title: title,
                    message: message
                });
            }

            if (type === "info") {
                this.$notify.info({
                    title: title,
                    message: message
                });
            }

            if (type === "warning") {
                this.$notify.warning({
                    title: title,
                    message: message
                });
            }

            if (type === "success") {
                this.$notify.success({
                    title: title,
                    message: message
                });
            }


        },

        //点击归还按钮
        clickReturn(row) {
            let url = "/borrow/admin/borrow";
            url += ("?id=" + row.id);

            axios.post(url, {}, {
                headers: {
                    'token': sessionStorage.getItem("token")
                }
            }).then(
                    () => {
                        this.openNotification("success", "成功", "归还成功");
                        this.listBorrow(this.pageNation, this.formInline);
                    })
                    .catch(
                            (err) => {
                                if (err.response.status === 401) {
                                    //跳转登录页面
                                    this.$router.push("/login");
                                } else {
                                    this.openNotification("error", "错误", "归还失败");
                                }
                            });
        },
    },

    mounted() {
        this.formInline.bookId = null;
        this.formInline.userId = null;
        this.formInline.status = null;
        try {
            this.listBorrow(this.pageNation, this.formInline);
        } catch (e) {
            console.error(e);
            this.openNotification("error", "错误", "前端错误")
        }
    }
};
</script>
